package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.JoinPicture;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.SundayMemberRecord;
import org.itboys.mobile.service.mongo.site.JoinSitePictureService;
import org.itboys.mobile.service.mongo.web.SundayHandleService;
import org.itboys.mobile.service.mongo.web.SundayMemberRecordService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/25
 * 描述：雄安加盟网_
 */
@Controller
@RequestMapping("/sunday/web/memberRecord")
public class SundayMemberRecordController extends BaseController{
    @Autowired
    private SundayMemberRecordService memberRecordService;
    @Autowired
    private SundayHandleService handleService;
    @Autowired
    private JoinSitePictureService pictureService;
    /**
     * 用户记录  主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                         @RequestParam(value="returnPage",required=false)Integer returnPage,
                         Model model){
        //会员用户主
        if(returnPage== SundayCommonConstants.user_type_member) {
            return "/sunday/member/record/index/member";
        }else if(returnPage== SundayCommonConstants.user_type_business){
            return "/sunday/member/record/index/business";
        }
        return "";
    }


    /**
     * 分页用户记录信息
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
        try {
            PageResult<SundayMemberRecord> result=memberRecordService.selectMember(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 查询用户详情
     * @param request
     * @param response
     */
    @RequestMapping("/findOne")
    public void findOne(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value="id",required=true)Long id,
                        @RequestParam(value="userType",required=false)Integer userType
    ) {
        try {
            SundayMemberRecord member = memberRecordService.findOne(id,userType);

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(member, CommonConstants.SUCCESS));

        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 用户详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            @RequestParam(value="userType",required=true)Integer userType,

            Model model){

        SundayMemberRecord member = memberRecordService.findOne(id,userType);
        model.addAttribute("member", member);


        if(userType==SundayCommonConstants.user_type_member){
            return "/sunday/member/record/input/member";
        }else if(userType== SundayCommonConstants.user_type_business){
            return "/sunday/member/record/input/business";
        }
        return "";
    }

    @RequestMapping("/picture")
    public String picture(
            HttpServletRequest request,
            @RequestParam(value="memberRecordId",required=true)Long memberRecordId,

            Model model){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("memberRecordId",memberRecordId);
        SundayMemberRecord webBusinessRecord=memberRecordService.getById(memberRecordId);
        model.addAttribute("webBusinessRecord",webBusinessRecord);
        List<JoinPicture> pictures=pictureService.list(param);
        model.addAttribute("pictures", pictures);

        return "/sunday/member/record/picture";
    }

    /**
     * 新增或修改用户
     * @param request
     * @param response
     * @param member

     */
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @ModelAttribute SundayMemberRecord member,
                      @RequestParam(value="imageFile",required=false) MultipartFile imageFile,
                      @RequestParam(value="backImageFile",required=false) MultipartFile backImageFile
    ) {
        try {
            memberRecordService.saveMember(member,imageFile,backImageFile);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/memberRecord/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id


    ) {
        try {
            memberRecordService.deleteMemberRecordAndProduct(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/memberRecord/handle------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 审核用户记录
     * @param request
     * @param response
     * @param memberRecordId
     */
    @RequestMapping("/handle")
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="memberRecordId",required=true) Long memberRecordId


    ) {
        try {
            handleService.handle(memberRecordId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/memberRecord/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    @RequestMapping("/handleAptitude")
    public void handleAptitude(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="memberRecordId",required=true) Long memberRecordId,
                       @RequestParam(value="type",required=true) Integer type


    ) {
        try {
            handleService.handleAptitude(memberRecordId,type);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/memberRecord/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}
